package com.dang.alex.dao.Imp;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.dang.alex.dao.ProductDAO;
import com.dang.alex.model.Book;
import com.dang.alex.model.Product;
import com.dang.alex.util.DBConnection;
import com.mysql.jdbc.Connection;

public class ProductDAOImp implements ProductDAO{
	
	private final static String FIND_BY_ID = "select * from d_product p left join d_book b on b.id = p.id where p.id = ?";
	private final static String FIND_BY_HOT = "select sum(product_num) as num,p.*,b.* "
												+ "from d_item i " 
												+ "join d_product p "
												+ "on i.product_id = p.id "
												+ "join d_book b "
												+ "on i.product_id = b.id "
												+ "group by product_id " + "order by num desc limit 0,?";
	private final static String FIND_BY_NEW = "select p.*,b.* " + "from d_product p " + "join d_book b " + "on p.id = b.id " + "order by p.add_time desc " + "limit 0,?";
	
	public Book findById(int id) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		Book book= null;
		ResultSet rst = null;
		try {
			conn = (Connection) DBConnection.getConnection();
			prep = conn.prepareStatement(FIND_BY_ID);
			prep.setInt(1, id);
			rst = prep.executeQuery();
			if(rst.next()) {
				book = new Book();
				book.setAuthor(rst.getString("author"));
				book.setAuthorSummary(rst.getString("author_summary"));
				book.setCatalogue(rst.getString("catalogue"));
				book.setId(rst.getInt("id"));
				book.setIsbn(rst.getString("isbn"));
				book.setPrintNumber(rst.getString("print_number"));
				book.setPrintTime(rst.getLong("print_time"));
				book.setPublishing(rst.getString("publishing"));
				book.setPublishDt(rst.getLong("publish_time"));
				book.setTotalPage(rst.getString("total_page"));
				book.setWhichEdtion(rst.getString("which_edtion"));
				book.setWordNumber(rst.getString("word_number"));
				
				book.setProductName(rst.getString("product_name"));
				book.setDescription(rst.getString("description"));
				book.setAddTime(rst.getLong("add_time"));
				book.setFixedPrice(rst.getDouble("fixed_price"));
				book.setDangPrice(rst.getDouble("dang_price"));
				book.setKeywords(rst.getString("keywords"));
				book.setHasDeleted(rst.getInt("has_deleted"));
				book.setProductPic(rst.getString("product_pic"));
				
			}
		}
		finally {
			DBConnection.close(prep, conn);
		}
		return book;
	}
	
	public List<Product> findByHot(int num) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		List<Product> pros = new ArrayList<Product>();
		ResultSet rst = null;
		try {
			prep = DBConnection.getConnection().prepareStatement(FIND_BY_HOT);
			prep.setInt(1, num);
			rst = prep.executeQuery();
			while (rst.next()) {
				Book book = new Book();
				book.setId(rst.getInt("id"));
				book.setProductName(rst.getString("product_name"));
				book.setFixedPrice(rst.getDouble("fixed_price"));
				book.setDangPrice(rst.getDouble("dang_price"));
				book.setProductPic(rst.getString("product_pic"));
				book.setAddTime(rst.getLong("add_time"));
				book.setAuthor(rst.getString("author"));
				book.setPublishing(rst.getString("publishing"));
				book.setPublishDt(rst.getLong("publish_time"));
				book.setAuthorSummary(rst.getString("author_summary"));
				book.setCatalogue(rst.getString("catalogue"));
				book.setDescription(rst.getString("description"));
				book.setHasDeleted(rst.getInt("has_deleted"));
				book.setIsbn(rst.getString("isbn"));
				book.setKeywords(rst.getString("keywords"));
				book.setPrintNumber(rst.getString("print_number"));
				book.setPrintTime(rst.getLong("print_time"));
				book.setPublishing(rst.getString("publishing"));
				book.setTotalPage(rst.getString("total_page"));
				book.setWhichEdtion(rst.getString("which_edtion"));
				book.setWordNumber(rst.getString("word_number"));
				
				pros.add(book);
			}
		}
		finally {
			DBConnection.close(rst, prep, conn);
		}
		return pros;
	}
	
	public List<Product> findByNew(int num) throws Exception {
		PreparedStatement prep = null;
		Connection conn = null;
		List<Product> pros = new ArrayList<Product>();
		ResultSet rst = null;
		try {
			prep = DBConnection.getConnection().prepareStatement(FIND_BY_NEW);
			prep.setInt(1, num);
			rst = prep.executeQuery();
			while (rst.next()) {
				Book book = new Book();
				book.setId(rst.getInt("id"));
				book.setProductName(rst.getString("product_name"));
				book.setFixedPrice(rst.getDouble("fixed_price"));
				book.setDangPrice(rst.getDouble("dang_price"));
				book.setProductPic(rst.getString("product_pic"));
				book.setAddTime(rst.getLong("add_time"));
				book.setAuthor(rst.getString("author"));
				book.setPublishing(rst.getString("publishing"));
				book.setPublishDt(rst.getLong("publish_time"));
				book.setAuthorSummary(rst.getString("author_summary"));
				book.setCatalogue(rst.getString("catalogue"));
				book.setDescription(rst.getString("description"));
				book.setHasDeleted(rst.getInt("has_deleted"));
				book.setIsbn(rst.getString("isbn"));
				book.setKeywords(rst.getString("keywords"));
				book.setPrintNumber(rst.getString("print_number"));
				book.setPrintTime(rst.getLong("print_time"));
				book.setPublishing(rst.getString("publishing"));
				book.setTotalPage(rst.getString("total_page"));
				book.setWhichEdtion(rst.getString("which_edtion"));
				book.setWordNumber(rst.getString("word_number"));
				
				pros.add(book);
			}
		}
		finally {
			DBConnection.close(rst, prep, conn);
		}
		return pros;
	}
		
	
}
